/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
/**
 * $Id: base64.hpp,v 1.8 2009-05-13 07:18:25 amaula Exp $
 * \file
 * \brief Base64 encoder/decoder header.
 * \author Antti Maula <antti.maula@tkk.fi>
 */
#ifndef _GIM_UTILS_BASE64_HPP_
#define _GIM_UTILS_BASE64_HPP_
namespace gim {

  /** Base64 data encoder/decoder.
   *
   * This class implements functionality for encoding/decoding data
   * from/to Base64 data format. See description of Base64 format from:
   * http://en.wikipedia.org/wiki/Base64
   */
  class CBase64 
  {
  public:
    /** Constructor.
     */
    CBase64();


    /** Destructor.
     */
    ~CBase64();
    

    /** Copy constructor.
     *
     * Always copies contents. (No references)
     */
    CBase64(const CBase64 &aSrc);


    /** Assignment operator.
     *
     * Always copies contents. (No references)
     */
    CBase64 &operator=(const CBase64 &aSrc);
    

    /** Encode given plaintext data to Base64 data.
     *
     * This function encodes the given data and stores the encoded
     * data inside the class instance.
     *
     * @param[in] aData         Pointer to data to encode. Can be binary.
     * @param[in] aDataSize     Number of bytes to encode.
     * @param[in] aCopyData     If this is set to 'true', the provided data
     *                          pointer contents are copied inside this class instance. If
     *                          this parameter is false, the provided pointer is directly
     *                          used as source.
     * @return                  Returns 'true'.
     */
    bool Encode(const unsigned char *aData, const unsigned int aDataSize, bool aCopyData = false);
    
    
    /** Decode given Base64 data to plaintext data.
     *
     * This function decodes the given Base64 data to decoded
     * form. The resulting data can be binary.
     *
     * @param[in] aData         Pointer to Base64 encoded data to decode.
     * @param[in] aDataSize     Size of data to decode.
     * @param[in] aCopyData     If this is set to 'true', the provided data
     *                          pointer contents are copied inside this class instance. If
     *                          this parameter is false, the provided pointer is directly
     *                          used as source.
     * @return                  'true' when the buffer was succesfully decoded. 'false'
     *                          when the given data was not Base64 compatible. 
     */
    bool Decode(const char *aData, const unsigned int aDataSize, bool aCopyData = false);
    

    /** Return pointer to encoded (Base64) data.
     *
     * @return                  Returns constant pointer to data container inside class instance.
     */
    const char *GetEncodedBufferPtr(void) const {
      return iEncodedBuffer;
    }


    /** Return number of bytes in encoded (Base64) buffer.
     *
     * @return                  Number of bytes currently in the encoded data buffer.
     */
    unsigned int GetEncodedBufferSize() const {
      return iEncodedBufferSize;
    }


    /** Return pointer to decoded (plaintext) data.
     *
     * @return                  Pointer to decoded data buffer container inside class instance.
     */
    const unsigned char *GetDecodedBufferPtr(void) const {
      return iDecodedBuffer;
    }


    /** Return number of bytes in decoded (plaintext) data buffer.
     *
     * @return                  Number of bytes currently in the decoded data buffer.
     */
    unsigned int GetDecodedBufferSize(void) const {
      return iDecodedBufferSize;
    }

  private:
    unsigned char *iDecodedBuffer; ///< Decoded data buffer
    unsigned int iDecodedBufferSize; ///< Number of bytes in decoded data buffer
    bool iDecodedBufferReadOnly; ///< Is the decoded buffer readonly?
    char *iEncodedBuffer; ///< Encoded data buffer
    unsigned int iEncodedBufferSize; ///< Encoded data buffer size
    bool iEncodedBufferReadOnly; ///< Is the encoded data buffer readonly.
  };
};
#endif
